<?php
 class Penjualan extends CI_Controller{
    
	function  __construct() {
        parent::__construct();
		$this->load->model('User_model');
        $this->load->model('Week_model');
        $this->load->model('Cabang_model');
        $this->load->model('Penjualan_model');
		$this->load->model('Barang_model');
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
	}
   
    function index(){
		$this->load->view('admin_views/penjualan/index');
    }
	
	function add()
	{
		$this->form_validation->set_rules('strkode_dealer', 'Kode Member', 'trim|required|xss_clean');
		$this->form_validation->set_rules('strno_ktp', 'No KTP', 'trim|required|xss_clean');
		$this->form_validation->set_rules('strnama_dealer', 'Nama Member', 'trim|required|xss_clean');
		$this->form_validation->set_rules('strkode_upline', 'Kode Upline', 'trim|required|xss_clean');
		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');
				
		if ($this->form_validation->run() == FALSE)
		{
			
			
			$bank = $this->Penjualan_model->selectBank();
			foreach ($bank as $g)
			{
				$data['intid_bank'][]	 	= $g->intid_bank;
				$data['strnama_bank'][] 	= $g->strnama_bank;
			}
			
			$starterkit = $this->Penjualan_model->selectStarterkit();
			foreach ($starterkit as $g)
			{
				$data['idstarterkit'][]		= $g->intid_barang;
				$data['namastarterkit'][] 	= $g->strnama;
				$data['id_harga'][] 		= $g->intid_harga;
				$data['hargajawa'][] 		= $g->intharga_jawa;
				$data['hargaluarjawa'][] 	= $g->intharga_luarjawa;
			}
			
			$cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			
			//$kodemax = $this->Penjualan_model->selectmaxkode();
			//$max = substr($kodemax[0]->kodemax, 1,8)+1;
			
			$getmember = $this->Penjualan_model->getNoMemberNew();
			$nilai = $getmember[0]->id;
			$id = $nilai + 1;
			$this->Penjualan_model->getNoMemberUpdate($id);
			
			$data['kode_dealer']= "M".$nilai;			
			$this->load->view('admin_views/penjualan/penjualan', $data);
			
		
		}else {
		$cek = $this->Penjualan_model->cek_strkodedelaer( $this->input->post('strkode_dealer'));
			if($cek == 0 ){
				$this->Penjualan_model->insert($_POST);
				$id =$this->db->insert_id();
			}else{
				$id = $this->Penjualan_model->get_iddealer( $this->input->post('strkode_dealer'));
			}
			$query = $this->Penjualan_model->selectMember($id); 
			foreach ($query as $g)
				{
					$data['strnama_dealer']	 			= $g->strnama_dealer;
					$data['strnama_upline']	 			= $g->strnama_upline;
					$data['strnama_unit']	 			= $g->strnama_unit;
					$data['strnama_cabang']	 			= $g->strnama_cabang;
					$data['strnama']	 		    	= $g->strnama;
					$data['intid_wilayah']	 			= $g->intid_wilayah;
					$data['intharga_jawa']	 			= $g->intharga_jawa;
					$data['intharga_luarjawa']	 		= $g->intharga_luarjawa;
					$data['intid_unit']	 				= $g->intid_unit;
					$data['intid_cabang']	 	    	= $g->intid_cabang;
					$data['intid_dealer']	 	    	= $g->intid_dealer;
					$data['intkode_cabang']	 	    	= $g->intkode_cabang;
					$data['intid_starterkit']	 	    = $g->intid_starterkit;
					$data['intharga']	 	    	= $g->intharga_jawa;
				
				}
			$data['default'] = $this->Penjualan_model->selectKelengkapanSK($query[0]->intid_starterkit);
			$week = $this->Penjualan_model->selectWeek();
			$data['intid_week'] = $week[0]->intid_week;
			$data['max_id'] = $this->getno_nota();		
			$data['strkode_upline'] = $this->input->post('strkode_upline');
			
			$this->load->view('admin_views/penjualan/print_nota', $data);
			
		}
	}	
	
	function lookup(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectCabang($keyword); 
        if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array( 
                                        'id'=>$row->intid_cabang,
                                        'value' => $row->strnama_cabang,
                                        ''
                                     );  
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	
	function lookupCabangBs(){
		$keyword = $this->input->post('term');
		$user = $this->input->post('state');
        $data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectCabangBs($keyword, $user); 
        if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array( 
                                        'id'=>$row->intid_cabang,
                                        'value' => $row->strnama_cabang,
                                        ''
                                     );  
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	
	function lookupUnit(){
		
        $keyword = $this->input->post('term');
        $data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectUnit($keyword); 
		if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array( 
                                        'id'=>$row->intid_unit,
                                        'value' => $row->strnama_unit,
                                        'value1' => $row->strnama_dealer,
					'value2' => $row->strkode_dealer,
                                     );  
            }
			           
        }
		
		 
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}

	function lookupUnitPerCabang001(){
		
        $keyword = $this->input->post('term');
	$cabang = $this->session->userdata('username');
        $data['response'] = 'false';
	if ($cabang == 'admin') {
		$query = $this->Penjualan_model->selectUnit($keyword);
	} else {
        	$query = $this->Penjualan_model->selectUnitPerCabang001($keyword, $cabang);
	} 
		if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array( 
                                        'id'=>$row->intid_unit,
                                        'value' => $row->strnama_unit,
                                        'value1' => $row->strnama_dealer,
					'value2' => $row->strkode_dealer,
                                     );  
            }
			           
        }
		
		 
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	
	function lookupUpline(){
		$keyword = $this->input->post('term');
		$unit = $this->input->post('state');
		$data['response'] = 'false'; 
		$query = $this->Penjualan_model->selectUpline($keyword, $unit); 
        if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array( 
                                        'id'=>$row->strkode_dealer,
										'intid_dealer' => $row->intid_dealer,
                                        'value' => strtoupper($row->strnama_dealer),
                                        'value1' => $row->strkode_upline,
					'value2' => $row->strnama_upline,
					'value3' => $row->intlevel_dealer,
					'value4' => $row->steamer,
					'value5' => $row->emc,
					'value6' => $row->chooper,
					'value7' => $row->metal_5lt,
					'value8' => $row->metal_7lt,
					'value9' => $row->metal_7in1,
                                     );  
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	
    function lookupUplineCalon(){
		$keyword = $this->input->post('term');
		$unit = $this->input->post('state');
		$data['response'] = 'false';
		$query = $this->Penjualan_model->selectUplineCalon($keyword, $unit); 
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->strkode_dealer,
                                        'value' => $row->strnama_dealer,
                                        'value1' => $row->strkode_upline,
										'value2' => $row->strnama_upline,
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); 

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
	function getno_nota(){
        $cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $week = $this->Penjualan_model->selectWeek();
        /*$max = $this->Penjualan_model->get_MaxNota()->result();
        $getnota = $this->Penjualan_model->getNoNota($max[0]->intid_nota);
		if(empty($getnota[0]->intno_nota)){
			$nilai =1;
		} else {
			$nilai = $getnota[0]->intno_nota;
		}*/

        $getnota = $this->Penjualan_model->getNoNotaNew();
		$nilai = $getnota[0]->id;
		$id = $nilai + 1;
		$this->Penjualan_model->getNoNotaUpdate($id);
		//$noUrut = (int) substr($nilai, 6, 9);
        //$noUrut++;
        //$kode = $cabang[0]->intkode_cabang.".".$week[0]->intid_week.".".sprintf("%05s", $noUrut);
		$kode = $cabang[0]->intid_cabang.".".$week[0]->intid_week.".".sprintf("%05s", $nilai);
        return $kode;

    }

    function getno_notaArisan(){
        $cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $week = $this->Penjualan_model->selectWeek();
        $max = $this->Penjualan_model->get_MaxNotaArisan()->result();
        $getnota = $this->Penjualan_model->getNoNotaArisan($max[0]->intid_nota);
        if(empty($getnota[0]->intid_nota)){
			$nilai =1;
		} else {
			$nilai = $getnota[0]->intno_nota;
		}

        $noUrut = (int) substr($nilai, 5, 9);
        $noUrut++;
        $kode = $cabang[0]->intkode_cabang.".".$week[0]->intid_week.".".sprintf("%05s", $noUrut);
        return $kode;

    }
	
	function getno_notadp(){
        $cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $week = $this->Penjualan_model->selectWeek();
        /*$max = $this->Penjualan_model->get_MaxNotaDp()->result();

        $getnota = $this->Penjualan_model->getNoNotaDp($max[0]->intid_nota);

        if(empty($getnota[0]->intid_nota)){
			$nilai =1;
		} else {
			$nilai = $getnota[0]->intno_nota;
		}

        $noUrut = (int) substr($nilai, 8, 5);
        $noUrut++;
        $kode = "DP.".$cabang[0]->intkode_cabang.".".$week[0]->intid_week.".".sprintf("%05s", $noUrut);*/
		$getnota = $this->Penjualan_model->getNoNotaDp();
		$nilai = $getnota[0]->id;
		$id = $nilai + 1;
		$this->Penjualan_model->getNoNotaDpUpdate($id);
		$kode = "DP.".$cabang[0]->intid_cabang.".".$week[0]->intid_week.".".sprintf("%05s", $nilai);

        return $kode;

    }
	
	function nota(){
        
        
		$this->form_validation->set_rules('intno_nota', 'No Nota', 'trim|required|xss_clean');
		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');
		if ($this->form_validation->run() == FALSE)
		{
            
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;


			$data['user'] = $this->session->userdata('username');
            $data['max_id'] = $this->getno_nota();
			
			$jpenjualan = $this->Penjualan_model->selectJPenjualan();
			foreach ($jpenjualan as $g)
			{
				$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
				$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
			}
			
			$this->load->view('admin_views/penjualan/nota', $data);
		 }else{
             $hal = $this->input->post('halaman');
			if(empty($hal)){
				$hal = "";
			}else{
				$hal = $this->input->post('halaman');
			}
            //$this->Penjualan_model->insertNota($_POST);
            $this->Penjualan_model->insertNotaHal($_POST,$hal);
           
            $max = $this->Penjualan_model->get_MaxNota()->result();
            $id = $max[0]->intid_nota;

			$data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
				if(!empty($data[$i]['intid_id'])){
				$detail = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data[$i]['intid_id'],
								'intquantity'		    => $data[$i]['intquantity'],
								'intid_harga'		    => $data[$i]['intid_id'],
								'intharga'				=> $data[$i]['intid_harga'],
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);
				 $this->Penjualan_model->add($detail);
				if($this->input->post('buat_arisan')!="arisan") {
				 $this->Penjualan_model->addStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
				}
				 }
            }
             $data_free = $this->input->post('barang_free');
				 if(sizeof($data_free) <= 1){
					 $d = sizeof($data_free) + 1;
				 }else{
					 $d = sizeof($data_free);
				 }
				 //untuk sementara
				 if(isset($data_free[$i]['nomor_nota'])){
					$temp = $data_free[$i]['nomor_nota'];
				 }else{
					$temp = "";
				 }
             for($i=1;$i<=30;$i++){
				 if(!empty($data_free[$i]['intid_id'])){
				 
				 $detail_free = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data_free[$i]['intid_id'],
								'intquantity'		    => $data_free[$i]['intquantity'],
								'is_free' 				=> 1,
								'intid_harga'			=> 0,
								'nomor_nota'			=> $temp
								);
				 $this->Penjualan_model->add($detail_free);
				if($this->input->post('buat_arisan')!="arisan") {
				 $this->Penjualan_model->addStok($data_free[$i]['intid_id'], $data_free[$i]['intquantity'], $this->input->post('intid_cabang'),$this->input->post('intno_nota'));
				}
				 }
               
		 	}

		//001
		$smartspending = $this->input->post('chkSmart');
		if ($smartspending == "on") {
			redirect('penjualan/cetak_nota_asi');
		}
		
			
            if($this->input->post('radio')==6){
                $ci = '5';
            }else{
                $ci='7';
            }

            //pilih cicilan
            if($this->input->post('buat_arisan')=="arisan"){

                    $arisan = array(
                                    'intid_arisan_detail' 	=> $id,
                                    'intuang_muka'		    => $this->input->post('intuangmukahide'),
                                    'intcicilan'            => $this->input->post('intcicilanhide'),
                                    'intjeniscicilan'		=> $ci,
                                    'group'                 => $this->input->post('group')
                                    );
                    $this->Penjualan_model->add_arisan($arisan);
                    $id_arisan =$this->db->insert_id();
                    $bulan = date('m');
                    $tahun = date('Y');
                    $ket = 'Pembayaran Cicilan langsung Ketika Daftar';
                    $ket_um = 'Pembayaran Uang Muka';
                    $this->Penjualan_model->add_arisan_detail_um($id_arisan, $ket_um);
                    $cicil_cek = $this->input->post('intc');
                    for ($v=1;$v<=7;$v++){
                         if(!empty($cicil_cek[$v])){
                            $this->Penjualan_model->add_arisan_detail_langsung($id_arisan, $ket);
                         }
                    }
			 }
            
            
            redirect('penjualan/cetak_nota');
			
		}
    }

    function cetak_nota(){
        $max = $this->Penjualan_model->get_MaxNota()->result();
        $id = $max[0]->intid_nota;
        $ada = $this->Penjualan_model->get_CetakNota($id);
        $data['default'] = $this->Penjualan_model->get_CetakNota($id);
        $this->load->view('admin_views/penjualan/cetak_nota', $data);
	}
	
	function cetak_nota_asi(){
        $max = $this->Penjualan_model->get_MaxNota()->result();
        $id = $max[0]->intid_nota;
        $ada = $this->Penjualan_model->get_CetakNota($id);
        $data['default'] = $this->Penjualan_model->get_CetakNota($id);
        $this->load->view('admin_views/penjualan/cetak_nota_asi', $data);
	}
	
	function lookupBarang(){
		$keyword = $this->input->post('term');
		$jbarang = $this->input->post('state');
        $data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectBarang($keyword, $jbarang); 
		if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
					$um = $row->intum_jawa;
					$cicilan = $row->intcicilan_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
					$um = $row->intum_luarjawa;
					$cicilan = $row->intcicilan_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->strnama,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
										'value5' => $um,
										'value6' => $cicilan,
                                        'value7' => $row->intid_harga,
                                     );
            }
			           
        }
		
		 
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	
	function lookupBarangPromo20(){
		$keyword = $this->input->post('term');
		$jbarang = $this->input->post('state');
        $data['response'] = 'false';
        $query = $this->Penjualan_model->selectBarangPromo20($keyword, $jbarang);
		if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {

                 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
					$um = $row->intum_jawa;
					$cicilan = $row->intcicilan_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
					$um = $row->intum_luarjawa;
					$cicilan = $row->intcicilan_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->promo,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
										'value5' => $um,
										'value6' => $cicilan,
                                        'value7' => $row->intid_harga,
                                     );
            }

        }


        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
	function lookupBarangFree20(){
		$keyword = $this->input->post('term');
        $state = $this->input->post('state');
		$data['response'] = 'false';
        $query = $this->Penjualan_model->selectBarangFree20($keyword, $state);
		if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {

                 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->promo,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
                                     );
            }

        }


        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
	function lookupBarangPromo10(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Penjualan_model->selectBarangPromo10($keyword);
		if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {

                 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
					$um = $row->intum_jawa;
					$cicilan = $row->intcicilan_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
					$um = $row->intum_luarjawa;
					$cicilan = $row->intcicilan_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->promo,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
										'value5' => $um,
										'value6' => $cicilan,
                                        'value7' => $row->intid_harga,
                                     );
            }

        }


        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
	function lookupBarangFree10(){
		$keyword = $this->input->post('term');
        $state = $this->input->post('state');
		$data['response'] = 'false';
        $query = $this->Penjualan_model->selectBarangFree10($keyword, $state);
		if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {

                 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
                }
                $data['message'][] = array(
                                       'id'=>$row->intid_barang,
                                       'value'=>$row->promo,
                                       'value1' => $hrg,
                                       'value2' => $row->intharga_luarjawa,
                                       'value3' => $pv,
                                       'value4' => $row->intpv_luarjawa,
                                     );
            }

        }


        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}

//001 PROMO MINI
	function lookupBarangPromoMini(){
		$keyword = $this->input->post('term');
		$jbarang = $this->input->post('state');
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $data['response'] = 'false';
        $query = $this->Penjualan_model->selectBarangPromoMini($keyword, $cabang[0]->intid_cabang, $jbarang);
		if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
					$um = $row->intum_jawa;
					$cicilan = $row->intcicilan_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
					$um = $row->intum_luarjawa;
					$cicilan = $row->intcicilan_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->promo,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
										'value5' => $um,
										'value6' => $cicilan,
                                        'value7' => $row->intid_harga,
                                     );
            }

        }


        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
	function saveNota()
	{
		$this->Penjualan_model->insertSaveNota($_POST);
		$id =$this->db->insert_id();
		$intid_barang = $this->input->post('intid_barang');
		$i = $this->db->query("select intid_harga from harga where intid_barang  = $intid_barang");
        $intid_harga = $i->result();
		$data1 = array(
            'intid_nota' => $id,
            'intid_barang' => $this->input->post('intid_barang'),
			'intquantity' => $this->input->post('intquantity'),
			'intid_harga' => $intid_harga[0]->intid_harga
            
		);
		$this->Penjualan_model->add($data1);
		$this->Penjualan_model->addStok($this->input->post('intid_barang'), $this->input->post('intquantity'), $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
		
		$j = $this->db->query("select intid_barang from starter_kit where intid_barang_starterkit  = $intid_barang");
        $result = $j->result();
		for($i=0;$i<=sizeof($result);$i++){
			if(!empty($result[$i]->intid_barang)){
				$detail = array(
						'intid_nota' 			=> $id,
						'intid_barang'	        => $result[$i]->intid_barang,
						'intquantity'		    => 1,
						'is_free' 				=> 1
				);
				$this->Penjualan_model->add($detail);
				$this->Penjualan_model->addStok($result[$i]->intid_barang, 1, $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
			}
		}	
		$row = $this->Penjualan_model->get_fromkodemember($this->input->post('intid_dealer'));		
		//menambah ke tabel rekrut maksimal 2//
		$this->Penjualan_model->insertGroupRekrutHadiah($this->input->post('strkode_upline'),$row[0]->strkode_dealer,$this->input->post('intno_nota'),'4',$this->input->post('intharga'));			
		
		redirect('penjualan/cetak_stater');
	}

     function cetak_stater(){
        $max = $this->Penjualan_model->get_MaxNota()->result();
        $id = $max[0]->intid_nota;
        $data['default'] = $this->Penjualan_model->get_CetakNotaMember($id);
        $this->load->view('admin_views/penjualan/cetak_starter', $data);
	}
	
	function lookupBarangLain(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectBarangLain($keyword); 
		if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {

                 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
                }
                $data['message'][] = array( 
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
                                     );  
            }
			           
        }
		
		 
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	
/*______________________________________________________________________
|									|
|									|
|									|
|									|
|				Kode Baru				|
|									|
|									|
|									|
|______________________________________________________________________*/

	function lg()
	{
		if (!$_POST)
		{
			$cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$data['id_wilayah'] = $nm_cabang[0]->intid_wilayah;

			$data['user'] = $this->session->userdata('username');
            $data['max_id'] = $this->getno_nota();
			
			$this->load->view('admin_views/penjualan/nota_lg', $data);
		 }else{
		 
		 	$id = $this->Penjualan_model->insertNotaHal($_POST,'LG');
			
			//ini penting bwt dirubah
			/*
			$r = $this->Penjualan_model->cekNota001($this->input->post('strkode_dealer'), $this->input->post('id_unit'), $this->input->post('totallg001'));
			for($i=0;$i<=sizeof($r);$i++){ 
				if(!empty($r[$i]->intid_nota)){	
					$this->Penjualan_model->updateNotaLg($r[$i]->intid_nota);
				}
			}
			*/
		
/*______________________________________________________________________
|									|
|									|
|									|
|									|
|			End of Kode Baru				|
|									|
|									|
|									|
|______________________________________________________________________*/
				
			//$max = $this->Penjualan_model->get_MaxNota()->result();
            //$id = $max[0]->intid_nota;
            $data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
            if(!empty($data[$i]['intid_id'])){
            $detail = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data[$i]['intid_id'],
								'intquantity'		    => $data[$i]['intquantity'],
								'intid_harga'		    => $data[$i]['intid_id'],
								'intharga'				=> $data[$i]['intid_harga'],
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);

             $this->Penjualan_model->add($detail);
			// $this->Penjualan_model->addStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
            }
            }
             $data_free = $this->input->post('barang_free');
             if(sizeof($data_free) <= 1){
                 $d = sizeof($data_free) + 1;
             }else{
                 $d = sizeof($data_free);
             }
             for($i=1;$i<=30;$i++){
				 
				
				 if(!empty($data_free[$i]['intid_id'])){
				 
				  
				 $detail_free = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data_free[$i]['intid_id'],
								'intquantity'		    => $data_free[$i]['intquantity'],
								'is_free' => 1,
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);
				 $this->Penjualan_model->add($detail_free);
				// $this->Penjualan_model->addStok($data_free[$i]['intid_id'], $data_free[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
				 }
				   
			 }
			 
		 $pilih = $this->input->post('pilih');
			$nomor_nota = $this->input->post('nomor_nota');
			for($i = 0; $i < sizeof($pilih);$i++){
				//echo ">>".$nomor_nota[$pilih[$i]]."<br />";
				if(!empty($nomor_nota[$pilih[$i]])){	
						$this->Penjualan_model->updateNotaLg_intno_nota($nomor_nota[$pilih[$i]]);
				}
			}
		 
			redirect('penjualan/cetak_notalg');
		}
		
		
			 
		
	}
	
	function check_omset() {
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
           		$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            	$id_cabang = $nm_cabang[0]->intid_cabang;
				$id = $this->Penjualan_model->getidDealer($this->input->post('strkode_dealer'));
				//$data['omset'] = $this->Penjualan_model->getOmset($id[0]->intid_dealer, $id_cabang);
				$data['omset'] = $this->Penjualan_model->getOmset($id[0]->intid_dealer);
				
				$this->load->view('admin_views/penjualan/omset', $data);
    }
	
	function tebuslg(){
		$this->load->view('admin_views/penjualan/tebuslg');
	}
	
	function lookupBarangLg(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Penjualan_model->selectBarangLg($keyword);
		if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                   
                }else{
                    $hrg = $row->intharga_luarjawa;
                    
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->lg,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
									);
            }

        }


        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
	function cetak_notalg(){
        $max = $this->Penjualan_model->get_MaxNota()->result();
        $id = $max[0]->intid_nota;
        $ada = $this->Penjualan_model->get_CetakNota($id);
        $data['default'] = $this->Penjualan_model->get_CetakNota($id);
        $this->load->view('admin_views/penjualan/cetak_notalg', $data);
	}
	function dp_()
	{
		//kalau dibutuhkan password tinggal dibatasi dengan tanda //
		redirect('penjualan/dp');
		
		$data['error_message'] = "";
		if($this->input->post('grandpass')){
					$data['result'] = $this->Penjualan_model->get_password("dp", $this->input->post('grandpass'));
					if($data['result'] > 0){
						//$temp =md5('pass');
						redirect('penjualan/dp');
						//$this->load->view('admin_views/penjualan/mini_after_pass',$data);
						/*
						$cabang = $this->User_model->getCabang($this->session->userdata('username'));
						$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

						$data['cabang'] = $nm_cabang[0]->strnama_cabang;
						$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
						$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;


						$data['user'] = $this->session->userdata('username');
						$data['max_id'] = $this->getno_notadp();
						
						$jpenjualan = $this->Penjualan_model->selectJPenjualan();
						foreach ($jpenjualan as $g)
						{
							$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
							$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
						}
						
						$this->load->view('admin_views/penjualan/nota_dp', $data);
						*/
				}else{
					$data['error_message'] = "<div class='error_message'> login salah</div>";
					$this->load->view('admin_views/penjualan/dp_', $data);
					}
		}else{
			$this->load->view('admin_views/penjualan/dp_', $data);
		}
	}
	
	function dp(){
		if (!$_POST)
		{
            /*
			if($temp!=md5('pass')){
				redirect('penjualan/dp_');
			}
			*/
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;


			$data['user'] = $this->session->userdata('username');
            $data['max_id'] = $this->getno_notadp();
			
			$jpenjualan = $this->Penjualan_model->selectJPenjualan();
			foreach ($jpenjualan as $g)
			{
				$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
				$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
			}
			
			$this->load->view('admin_views/penjualan/nota_dp', $data);
		 }else{
            $this->Penjualan_model->insertNota($_POST);
			
			$max = $this->Penjualan_model->get_MaxNotaDp()->result();
            $id = $max[0]->intid_nota;
            $data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
            if(!empty($data[$i]['intid_id'])){
            $detail = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data[$i]['intid_id'],
								'intquantity'		    => $data[$i]['intquantity'],
								'intid_harga'		    => $data[$i]['intid_id'],
								'intharga'				=> $data[$i]['intid_harga'],
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);

             $this->Penjualan_model->add($detail);
            }
            }

             $data_free = $this->input->post('barang_free');
             if(sizeof($data_free) <= 1){
                 $d = sizeof($data_free) + 1;
             }else{
                 $d = sizeof($data_free);
             }
             for($i=1;$i<=30;$i++){
             
            
             if(!empty($data_free[$i]['intid_id'])){
             
              
             $detail_free = array(
							'intid_nota' 			=> $id,
							'intid_barang'	        => $data_free[$i]['intid_id'],
							'intquantity'		    => $data_free[$i]['intquantity'],
                            'is_free' 				=> 1,
							'intid_harga'			=> 0,
							'nomor_nota'			=> $data[$i]['nomor_nota']
							);
             $this->Penjualan_model->add($detail_free);
             }
               
		 	}
			redirect('penjualan/cetak_notadp');
		}
		
	}
	
	function cetak_notadp(){
        $max = $this->Penjualan_model->get_MaxNotaDp()->result();
        $id = $max[0]->intid_nota;
        $ada = $this->Penjualan_model->get_CetakNotaDp($id);
        $data['default'] = $this->Penjualan_model->get_CetakNotaDp($id);
        $this->load->view('admin_views/penjualan/cetak_notadp', $data);
	}
	
	function lunas() {
		
		$this->load->view('admin_views/penjualan/search_notadp');
	
	}
	
	function lookupDp(){
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
		$id_cabang = $nm_cabang[0]->intid_cabang;
		$keyword = $this->input->post('term');
        $data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectNotaDp($keyword, $id_cabang); 
		if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array( 
                                        'id'=>$row->intid_nota,
                                        'value' => $row->intno_nota,
                                        
                                     );  
            }
			           
        }
		
		 
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	
	function check_notadp() {
        if($this->input->post('ajax') == '1') {

           
                $data['datadp'] = $this->Penjualan_model->getNotaDp($this->input->post('intno_nota'));
							
				$this->load->view('admin_views/penjualan/check_notadp', $data);
           
        }
    }
	
	function pelunasan_dp() {
        if($this->input->post('ajax') == '1') {

           
                $datadp = $this->Penjualan_model->pelunasanNotaDp($this->input->post('no_notadp'));
				foreach ($datadp as $g)
				{
					$data['strnama_dealer']	 			= $g->strnama_dealer;
					$data['strnama_upline']	 			= $g->strnama_upline;
					$data['strkode_dealer']	 			= $g->strkode_dealer;
					$data['strnama_unit']	 			= $g->strnama_unit;
					$data['intomset10']	 				= $g->intomset10;
					$data['intomset20']	 				= $g->intomset20;
					$data['intdp']	 					= $g->intdp;
					$data['inttotal_omset']	 			= $g->inttotal_omset;
					$data['intkomisi10']	 			= $g->intkomisi10;
					$data['intkomisi20']	 			= $g->intkomisi20;
					$data['intpv']	 					= $g->intpv;
					$data['inttotal_bayar']	 			= $g->inttotal_bayar;
					$data['intsisa']	 				= $g->intsisa;
					$data['intid_nota']	 				= $g->intid_nota;
					
				
				}
				
				$data['detaildp'] = $this->Penjualan_model->pelunasanNotaDetailDp($this->input->post('no_notadp'));
				
				$cabang = $this->User_model->getCabang($this->session->userdata('username'));
            	$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            	$data['cabang'] = $nm_cabang[0]->strnama_cabang;
            	$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
				
				$week = $this->Penjualan_model->selectWeek();
        		$data['intid_week']= $week[0]->intid_week;


			    $data['max_id'] = $this->getno_nota();			
				$this->load->view('admin_views/penjualan/nota_lunasdp', $data);
           
        }
    }
	
	function lunasdp(){
		
		$this->Penjualan_model->updateNota($_POST);
		$id = $this->input->post('intid_nota');
		//input ke stok
		$stok = $this->Penjualan_model->GetBarang($id);
		$intid_id = $stok[0]->intid_barang;
        $intquantity = $stok[0]->intquantity;
		$this->Penjualan_model->addStok($intid_id, $intquantity, $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
		//end 
		//$ada = $this->Penjualan_model->get_CetakNota($id);
       	$data['default'] = $this->Penjualan_model->get_CetakNota($id);
        $this->load->view('admin_views/penjualan/cetak_lunasnota', $data);
		//redirect('penjualan/cetak_notalunasdp');
	}
	
	function cetak_notalunasdp($id){
        //$max = $this->Penjualan_model->get_MaxNota()->result();
        //$id = $max[0]->intid_nota;
		//echo $id = $this->input->post('intid_nota');
        $ada = $this->Penjualan_model->get_CetakNota($id);
       	$data['default'] = $this->Penjualan_model->get_CetakNota($id);
        //$this->load->view('admin_views/penjualan/cetak_lunasnota', $data);
	}
	/*
	//digantikan dengan arisan yang baru 28 agustus 2013
	function a_risan()
	{
          
		$this->form_validation->set_rules('intid_unit', 'Unit', 'trim|required|xss_clean');
		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');
		if ($this->form_validation->run() == FALSE)
		{
            
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;

			$data['user'] = $this->session->userdata('username');
            $data['max_id'] = $this->getno_nota();
			$group1 = $this->Penjualan_model->Limitgroupsatu();
			$data['group1'] = $group1[0]->group1;
			$group2 = $this->Penjualan_model->Limitgroupdua();
			$data['group2'] = $group2[0]->group2;
			$group3 = $this->Penjualan_model->Limitgrouptiga();
			$data['group3'] = $group3[0]->group3;
			$group4 = $this->Penjualan_model->Limitgroupempat();
			$data['group4'] = $group4[0]->group4;
			$group5 = $this->Penjualan_model->Limitgrouplima();
			$data['group5'] = $group5[0]->group5;
			$group6 = $this->Penjualan_model->Limitgroupenam();
			$data['group6'] = $group6[0]->group6;
			$group7 = $this->Penjualan_model->Limitgrouptujuh();
			$data['group7'] = $group7[0]->group7;
			$group8 = $this->Penjualan_model->Limitgroupdelapan();
			$data['group8'] = $group8[0]->group8;
			$group9 = $this->Penjualan_model->Limitgroupsembilan();
			$data['group9'] = $group9[0]->group9;
			$group10 = $this->Penjualan_model->Limitgroupsepuluh();
			$data['group10'] = $group10[0]->group10;
			$group11 = $this->Penjualan_model->Limitgroupsebelas();
			$data['group11'] = $group11[0]->group11;
			$group12 = $this->Penjualan_model->Limitgroupduabelas();
			$data['group12'] = $group12[0]->group12;
			$group13 = $this->Penjualan_model->Limitgrouptigabelas();
			$data['group13'] = $group13[0]->group13;
			$group14 = $this->Penjualan_model->Limitgroupempatbelas();
			$data['group14'] = $group14[0]->group14;
			$group15 = $this->Penjualan_model->Limitgrouplimabelas();
			$data['group15'] = $group15[0]->group15;
			$this->load->view('admin_views/penjualan/a_risan', $data);
		 }else{

            $this->Penjualan_model->insertArisan($_POST);
            $max = $this->Penjualan_model->get_MaxArisan()->result();
            $id = $max[0]->intid_darisan;
            $data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
               // print_r($data[$i]);
            if(!empty($data[$i]['intid_id'])){
            $detail = array(
							'intid_arisan_detail' => $id,
							'intuang_muka' 			=> $data[$i]['intuang_muka'],
							'intid_barang'	        => $data[$i]['intid_id'],
							'intcicilan'		    => $data[$i]['intcicilan'] - 1
                            );

             $this->Penjualan_model->addArisan($detail);
            }
            }
            redirect('admin/penjualan/a_risan');
			
		}
		
	}
	*/
	function a_risan()
	{
        $this->load->model('a_risan_model','a_m');

		$this->form_validation->set_rules('intid_unit', 'Unit', 'trim|required|xss_clean');
		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');
        
        if ($this->form_validation->run() == FALSE)
		{
          
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;

			$data['user'] = $this->session->userdata('username');
            $data['max_id'] = $this->getno_nota();
            //Proses Pengecekan
            
            // cek kondisi group_cek
            //// $select = 'select if(max(batas) >= ), from group_cek';
            // cek group_cek
            //$query = $this->barurekursif();
            //echo $query[0]->group;
            //echo $query[0]->batas;
            
           //buat select nya
            $data['cek2'] = $this->rekursif_group();
            
            $this->load->view('admin_views/penjualan/a_risan', $data);
		 }else{

            $this->Penjualan_model->insertArisan($_POST);
            $max = $this->Penjualan_model->get_MaxArisan()->result();
            $id = $max[0]->intid_darisan;
            $data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
               // print_r($data[$i]);
            if(!empty($data[$i]['intid_id'])){
            $detail = array(
							'intid_arisan_detail' => $id,
							'intuang_muka' 			=> $data[$i]['intuang_muka'],
							'intid_barang'	        => $data[$i]['intid_id'],
							'intcicilan'		    => $data[$i]['intcicilan'] - 1
                            );

             $this->Penjualan_model->addArisan($detail);
            }
            }
            redirect('admin/penjualan/a_risan');
			
		}
		
	}
	//untuk membantuk pencarian group arisan
	function rekursif_group(){
        $this->load->model('a_risan_model');
        $query = $this->a_risan_model->selectGroup();
       // echo $query[0]->batas;
       // echo date('Y-m-d');
        if($query[0]->batas <= date('Y-m-d') and $query[0]->total > 0){
            $data['group'] = $query[0]->group + 1;
            $data['month'] = date('m',strtotime($query[0]->batas))+1;
            $data['batas'] = $query[0]->batas;
            $this->a_risan_model->insertGroup($data);
            //echo "group";
            return $this->rekursif_group();
        }elseif($query[0]->total >= $query[0]->jumlah){
            $data['group'] = $query[0]->group + 1;
            $data['month'] = date('m',strtotime($query[0]->batas));
            $data['batas'] = $query[0]->batas;
            $this->a_risan_model->insertGroup($data);
          //  echo "group";
            return $this->rekursif_group();
        }elseif($query[0]->batas <= date('Y-m-d') and $query[0]->total == 0){
             $data['group'] = $query[0]->group;
            $data['month'] = date('m',strtotime($query[0]->batas))+1;
            $data['batas'] = $query[0]->batas;
            $this->a_risan_model->updateGroup($data);
          //  echo "group";
            return $this->rekursif_group();
        }else{
            //echo "yuhuu<br/>";
            return $query;
            }
    }
	///dimatikan untuk sementara
	/*
    function bayar_arisan() {
        $data['form_action']	= site_url('penjualan/bayar_arisan');
        $var = "bulan";
        $terpilih = date('m');
        $bulan = 1;
        $b=12;
        $data['combo_bulan'] = $this->combonamabln($bulan, $b, $var, $terpilih);
        // Load data
        $data['bulan'] = "";
        $data['b'] = "";
        $data['tahun'] = "";
        $data['group'] = "";
        $data['jenis'] = "";
		$data['max_id'] = $this->getno_nota();
        if($_POST) {

            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
   
            $id_cabang = $nm_cabang[0]->intid_cabang;
			$data['arisan'] = $this->Penjualan_model->get_all_arisan($_POST, $id_cabang);
            $data['bulan'] = $this->nama_bulan($this->input->post('bulan'));
            $data['b'] = $this->input->post('bulan');
            $data['tahun'] = $this->input->post('tahun');
            $data['group'] = $this->input->post('group');
            $data['jenis'] = $this->input->post('arisan');

            $ses = array(
						'bulan_session' => $this->input->post('bulan'),
						'tahun_session'=> $this->input->post('tahun'),
						'group_session' => $this->input->post('group')

					);
						$this->session->set_userdata($ses);
            
        }
        $this->load->view('admin_views/penjualan/bayar_arisan', $data);
    }
*/

function bayar_arisan() {
        $data['form_action']	= site_url('penjualan/bayar_arisan');
        $var = "bulan";
        $terpilih = date('m');
        $bulan = 1;
        $b=12;
        $data['combo_bulan'] = $this->combonamabln($bulan, $b, $var, $terpilih);
        // Load data
        $data['bulan'] = "";
        $data['b'] = "";
        $data['tahun'] = "";
        $data['group'] = "";
        $data['jenis'] = "";
		$data['max_id'] = $this->getno_nota();
        if($_POST) {
			if($this->input->post('id_unit')==""){
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
   
            $id_cabang = $nm_cabang[0]->intid_cabang;
			$data['arisan'] = $this->Penjualan_model->get_all_arisan($_POST, $id_cabang);
            $data['bulan'] = $this->nama_bulan($this->input->post('bulan'));
            $data['b'] = $this->input->post('bulan');
            $data['tahun'] = $this->input->post('tahun');
            $data['group'] = $this->input->post('group');
            $data['jenis'] = $this->input->post('arisan');

            $ses = array(
						'bulan_session' => $this->input->post('bulan'),
						'tahun_session'=> $this->input->post('tahun'),
						'group_session' => $this->input->post('group')

					);
						$this->session->set_userdata($ses);
			}else{
				$cabang = $this->User_model->getCabang($this->session->userdata('username'));
				$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
	   
				$id_cabang = $nm_cabang[0]->intid_cabang;
				$data['arisan'] = $this->Penjualan_model->get_all_arisan_ver1($_POST, $id_cabang);
				$data['bulan'] = $this->nama_bulan($this->input->post('bulan'));
				$data['b'] = $this->input->post('bulan');
				$data['tahun'] = $this->input->post('tahun');
				$data['group'] = $this->input->post('group');
				$data['jenis'] = $this->input->post('arisan');

				$ses = array(
							'bulan_session' => $this->input->post('bulan'),
							'tahun_session'=> $this->input->post('tahun'),
							'group_session' => $this->input->post('group')

						);
							$this->session->set_userdata($ses);				
			}            
        }
        $this->load->view('admin_views/penjualan/bayar_arisan', $data);
    }
    function ubah_pemenang($id, $group, $jenis, $bln, $tahun){
       $get_p = $this->Penjualan_model->pemenang_terakhir($id, $group);
	   $urutan = $get_p->urutan;
	   if($urutan==0){
		   $u=1;
		}else{
		   $u = $urutan + 1;
		}
        $this->Penjualan_model->update_pemenang($id, $u);
        
		/*$sisa_blmmenang = $this->Penjualan_model->getJumlahArisanGroup($id, $group, $jenis, $bln, $tahun);
		
		if($sisa_blmmenang->jum_sisa == 0){
			$sisa=1;
		}else{
			$sisa=$sisa_blmmenang->jum_sisa;
		}
		$data['sisa'] = $sisa;*/
		
		//tambahan insert ke table nota ketika update pemenang arisan
		$intno_nota = $this->getno_nota();
		$this->Penjualan_model->insertNotaArisanPemenang($intno_nota, $id);
//001
		$this->Penjualan_model->updateArisan001($id, $intno_nota);

		$data['default'] = $this->Penjualan_model->get_notaArisan($intno_nota);
		$this->load->view('admin_views/penjualan/cetak_pemenang', $data);
        
    }
	
	 function cetak_pemenang(){
        $max = $this->Penjualan_model->get_MaxNota()->result();
        $id = $max[0]->intid_nota;
        $data['default'] = $this->Penjualan_model->get_CetakArisan($id);
        $this->load->view('admin_views/penjualan/cetak_pemenang', $data);
	}
	
    function view_bayar($id) {
        $var = "bulan_bayar";
        $terpilih = date('m');
        $bulan = 1;
        $b=12;
        $data['combo_bulan'] = $this->combonamabln($bulan, $b, $var, $terpilih);
        $data['view_bayar'] = $this->Penjualan_model->viewDetailbayar($id);
		$data['total_bayar'] = $this->Penjualan_model->viewTotalbayar($id);
        $data['id_nota'] = $id;
		$data['max_id'] = $this->uri->segment(4);
		$this->load->view('admin_views/penjualan/view_detail_bayar', $data);
    }

    function hapus_arisan($id, $bulan, $tahun, $group) {
        $this->Penjualan_model->delete_arisan($id);
        $this->session->set_flashdata('message', '1 Data Arisan berhasil dihapus');
          
        redirect('penjualan/bayar_arisan');
       
    }

    function update_cicilan($id, $cicilan,$no_nota){
		$m = $this->Penjualan_model->viewTotalbayar($id);
        $this->Penjualan_model->update_cicilan($id, $cicilan, $no_nota);
    }
	
    function simpan_arisan($id) {
		$bulan = $this->input->post('bulan');
        $tahun = $this->input->post('tahun');
        $ket = $this->input->post('ket');
        if ($this->cek_arisan($id, $bulan, $tahun) === true)
		{
					$cek = $this->Penjualan_model->add_arisan_detail($id, $ket, $bulan, $tahun);
    				echo $cek;
		}
		else
		{
			echo "double";
		}
	}

	function cek_arisan($id, $bulan, $tahun){
		
		$cek = $this->Penjualan_model->cek_arisan($id, $bulan, $tahun)->num_rows();
		
		if ($cek > 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

    function nama_bulan($bulan) {
        switch ($bulan) {
            case '1':
                $b = "Januari";
                break;
            case '2':
                $b = "Februari";
                break;
            case '3':
                $b = "Maret";
                break;
            case '4':
                $b = "April";
                break;
            case '5':
                $b = "Mei";
                break;
            case '6':
                $b = "Juni";
                break;
            case '7':
                $b = "Juli";
                break;
            case '8':
                $b = "Agustus";
                break;
            case '9':
                $b = "September";
                break;
            case '10':
                $b = "Oktober";
                break;
            case '11':
                $b = "November";
                break;
            case '12':
                $b = "Desember";
                break;

        }

        return $b;

    }

    function combonamabln($awal, $akhir, $var, $terpilih) {
        $nama_bln=array(1=> "Januari", "Februari", "Maret", "April", "Mei",
                "Juni", "Juli", "Agustus", "September",
                "Oktober", "November", "Desember");
        $b='';
        $b.="<select name=$var id=$var>";
        for ($bln=$awal; $bln<=$akhir; $bln++) {
            if ($bln==$terpilih)
                $b.="<option value=$bln selected>$nama_bln[$bln]</option>";
            else
                $b.="<option value=$bln>$nama_bln[$bln]</option>";
        }
        $b.="</select> ";
        return $b;
    }

    
	function saveArisan()
	{
		$this->Penjualan_model->insertSaveArisan($_POST);
		redirect('penjualan/a_srisan');
	}
	
	function cetak_arisan(){
        $id = $this->uri->segment(3);
        $intno_nota = $this->uri->segment(4);
		$this->Penjualan_model->insertNotaArisan($intno_nota, $id);
		$data['default'] = $this->Penjualan_model->get_CetakArisan($id);
		$data['no_nota'] = $this->uri->segment(4);
		$this->load->view('admin_views/penjualan/cetak_arisan', $data);
	}
	
	function lookupKC(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectKC($keyword); 
        if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array( 
                                        'id'=>$row->intid_cabang,
                                        'value' => $row->strnama_cabang,
                                        ''
                                     );  
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	function lookupbabyKC(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectbabyKC($keyword); 
        if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array( 
                                        'id'=>$row->intid_cabang,
                                        'value' => $row->strnama_cabang,
                                        ''
                                     );  
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	function lookupSC(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectSC($keyword); 
        if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array( 
                                        'id'=>$row->intid_cabang,
                                        'value' => $row->strnama_cabang,
                                        ''
                                     );  
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	
	function spesial(){
        
        
		$this->form_validation->set_rules('intno_nota', 'No Nota', 'trim|required|xss_clean');
		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');
		if ($this->form_validation->run() == FALSE)
		{
            
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;


			$data['user'] = $this->session->userdata('username');
            $data['max_id'] = $this->getno_nota();
			
			$jpenjualan = $this->Penjualan_model->selectJPenjualanSpesial();
			foreach ($jpenjualan as $g)
			{
				$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
				$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
			}
			
			$this->load->view('admin_views/penjualan/spesial', $data);
		 }else{
             
            $this->Penjualan_model->insertNota($_POST);
            
            $max = $this->Penjualan_model->get_MaxNota()->result();
            $id = $max[0]->intid_nota;

            $data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
				if(!empty($data[$i]['intid_id'])){
				$detail = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data[$i]['intid_id'],
								'intquantity'		    => $data[$i]['intquantity'],
								'intid_harga'		    => $data[$i]['intid_id'],
								'nomor_nota'			=> $data[$i]['nomor_nota']								
								);
				 $this->Penjualan_model->add($detail);
	
				 $this->Penjualan_model->addStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
				 
				}
            }

             $data_free = $this->input->post('barang_free');
				 if(sizeof($data_free) <= 1){
					 $d = sizeof($data_free) + 1;
				 }else{
					 $d = sizeof($data_free);
				 }
             for($i=1;$i<=30;$i++){
				 if(!empty($data_free[$i]['intid_id'])){
				 
				  
				 $detail_free = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data_free[$i]['intid_id'],
								'intquantity'		    => $data_free[$i]['intquantity'],
								'is_free' => 1,
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);
				 $this->Penjualan_model->add($detail_free);
				 $this->Penjualan_model->addStok($data_free[$i]['intid_id'], $data_free[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
				 }
               
		 	}
            redirect('penjualan/cetak_nota');
			
		}
    }
	
	function istimewa(){
		
		$this->form_validation->set_rules('intno_nota', 'No Nota', 'trim|required|xss_clean');
		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');
		if ($this->form_validation->run() == FALSE)
		{
            
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;


			$data['user'] = $this->session->userdata('username');
            $data['max_id'] = $this->getno_nota();
			
			$jpenjualan = $this->Penjualan_model->selectJPenjualanNotTrade();
			foreach ($jpenjualan as $g)
			{
				$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
				$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
			}
			
			$this->load->view('admin_views/penjualan/istimewa', $data);
		 }else{
             
            $this->Penjualan_model->insertNota($_POST);
            
            $max = $this->Penjualan_model->get_MaxNota()->result();
            $id = $max[0]->intid_nota;


            $data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
				if(!empty($data[$i]['intid_id'])){
				$detail = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data[$i]['intid_id'],
								'intquantity'		    => $data[$i]['intquantity'],
								'intid_harga'		    => $data[$i]['intid_id'],
								'intharga'				=> $data[$i]['intid_harga'],
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);
				 $this->Penjualan_model->add($detail);
	
				 $this->Penjualan_model->addStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
				 
				}
            }

             $data_free = $this->input->post('barang_free');
				 if(sizeof($data_free) <= 1){
					 $d = sizeof($data_free) + 1;
				 }else{
					 $d = sizeof($data_free);
				 }
             for($i=1;$i<=30;$i++){
				 if(!empty($data_free[$i]['intid_id'])){
				 
				  
				 $detail_free = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data_free[$i]['intid_id'],
								'intquantity'		    => $data_free[$i]['intquantity'],
								'is_free' 				=> 1,
								'intid_harga'			=> 0,
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);
				 $this->Penjualan_model->add($detail_free);
				 $this->Penjualan_model->addStok($data_free[$i]['intid_id'], $data_free[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
				 }
               
		 	}
            redirect('penjualan/cetak_nota_asi');
			
		}
	}
	
	function lookupManager(){
		$keyword = $this->input->post('term');
		$data['response'] = 'false'; 
		$query = $this->Penjualan_model->selectManager($keyword); 
        if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array( 
                                        'id'=>$row->intid_unit,
                                        'value' => $row->strnama_dealer,
                                        'value1' => $row->strkode_upline,
										'value2' => $row->strnama_upline,
										'value3' => $row->intlevel_dealer,
									);  
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	
	function hadiah(){
		$this->load->model('PO_model_1');
		if (!$_POST)
		{
            
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;


			$data['user'] = $this->session->userdata('username');
            $data['max_id'] = $this->getno_nota();
			
			$dataTemp['name']	=	'jenis_nota';
			$dataTemp['id']	=	'jenis_nota';
			$data['jenis_nota'] = $this->PO_model_1->get_jenis_notaHadiah($dataTemp);
			$this->load->view('admin_views/penjualan/nota_hadiah', $data);
		 }else{

            $this->Penjualan_model->insertNotaHadiah($_POST);
			
			$max = $this->Penjualan_model->get_MaxNotaHadiah()->result();
            $id = $max[0]->intid_nota;
            $data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
            if(!empty($data[$i]['intid_id'])){
            $detail = array(
							'intid_nota' 			=> $id,
							'intid_barang'	        => $data[$i]['intid_id'],
							'intquantity'		    => $data[$i]['intquantity'],
							'ket'		   			=> $data[$i]['ket'],
                            				'nomor_nota'			=> $data[$i]['nomor_nota']
							);

             $this->Penjualan_model->addhadiah($detail);
			 
			 $this->Penjualan_model->addStokHadiah($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
            }
            }
			redirect('penjualan/cetak_notahadiah');
		}
		
	}
	
	function lookupBarangHadiah(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectBarangHadiah($keyword); 
		if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array(
                    'id'=>$row->intid_barang_hadiah,
					'value'=>$row->strnama
                                        
                );
            }
			           
        }
		if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	/*
	//yang lama tidak dipakai tgl 28 Agustus 2013 oleh ikhlas
	//diganti ama yang baru dibawahnya
	function cetak_notahadiah(){
        $max = $this->Penjualan_model->get_MaxNotaHadiah()->result();
        $id = $max[0]->intid_nota;
        $ada = $this->Penjualan_model->get_CetakNotaHadiah($id);
        $data['default'] = $this->Penjualan_model->get_CetakNotaHadiah($id);
        $this->load->view('admin_views/penjualan/cetak_notahadiah', $data);
	}
	*/
	function cetak_notahadiah(){
		$kode	=	$this->input->get('kode');
		if(empty($kode) or $kode == ''){
			$max = $this->Penjualan_model->get_MaxNotaHadiah()->result();
			$id = $max[0]->intid_nota;
			$ada = $this->Penjualan_model->get_CetakNotaHadiah($id);
			$data['default'] = $this->Penjualan_model->get_CetakNotaHadiah($id);
		}else{
			$data['default'] = $this->Penjualan_model->get_CetakNotaHadiah($kode);
		}
	  $this->load->view('admin_views/penjualan/cetak_notahadiah', $data);
	}
	function hitungomset()
	{
		if($this->input->post('ajax') == '1') {

                $omset = $this->Penjualan_model->cekOmset($this->input->post('no_nota'));
				echo "<input type='text' name='omset' size='10' value='".$omset[0]->inttotal_omset."'>";
        }
	}
	
	function promo()
	{
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
        $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;


		$data['user'] = $this->session->userdata('username');
        $data['max_id'] = $this->getno_nota();
			
		$jpenjualan = $this->Penjualan_model->selectJPenjualanPromo();
		foreach ($jpenjualan as $g)
		{
			$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
			$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
		}
		$this->load->view('admin_views/penjualan/promo', $data);
	}
	
	function lookupBarangFree(){
		$keyword = $this->input->post('term');
        $state = $this->input->post('state');
		$data['response'] = 'false';
        $query = $this->Penjualan_model->selectBarangFree($keyword, $state);
		if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {

                 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->promo,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
                                     );
            }

        }


        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
	function netto(){
        
        
		$this->form_validation->set_rules('intno_nota', 'No Nota', 'trim|required|xss_clean');
		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');
		if ($this->form_validation->run() == FALSE)
		{
            
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;


			$data['user'] = $this->session->userdata('username');
            $data['max_id'] = $this->getno_nota();
			
			$this->load->view('admin_views/penjualan/netto', $data);
		 }else{
             
            $this->Penjualan_model->insertNota($_POST);
            
            $max = $this->Penjualan_model->get_MaxNota()->result();
            $id = $max[0]->intid_nota;

			$data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
				if(!empty($data[$i]['intid_id'])){
				$detail = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data[$i]['intid_id'],
								'intquantity'		    => $data[$i]['intquantity'],
								'intid_harga'		    => $data[$i]['intid_id'],
								'intharga'				=> $data[$i]['intid_harga'],
								'nomor_nota'			=> $data[$i]['nomor_nota']								
								);
				 $this->Penjualan_model->add($detail);
	
				 $this->Penjualan_model->addStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
				 
				}
            }

             $data_free = $this->input->post('barang_free');
				 if(sizeof($data_free) <= 1){
					 $d = sizeof($data_free) + 1;
				 }else{
					 $d = sizeof($data_free);
				 }
             for($i=1;$i<=30;$i++){
				 if(!empty($data_free[$i]['intid_id'])){
				 
				 $detail_free = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data_free[$i]['intid_id'],
								'intquantity'		    => $data_free[$i]['intquantity'],
								'is_free' 				=> 1,
								'intid_harga'			=> 0,
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);
				 $this->Penjualan_model->add($detail_free);
				 $this->Penjualan_model->addStok($data_free[$i]['intid_id'], $data_free[$i]['intquantity'], $this->input->post('intid_cabang'),$this->input->post('intno_nota'));
				 }
               
		 	}
			
            redirect('penjualan/cetak_nota');
			
		}
    }

//999
	/////line ikhlas 11042013//
	function grand()
	{
		$data['error_message'] = "";
		if($this->input->post('grandpass')){
					$data['result'] = $this->Penjualan_model->get_password("grand", $this->input->post('grandpass'));
					if($data['result'] > 0){
					redirect('penjualan/grand_pass');
					
            redirect('penjualan/grand_pass');
				}else{
					$data['error_message'] = "<div class='error_message'> login salah</div>";
					$this->load->view('admin_views/penjualan/grand', $data);
					}
		}else{
			$this->load->view('admin_views/penjualan/grand', $data);
		}
	}
	/////line ikhlas 11042013//
	function grand_pass(){
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
					$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
			
					$data['cabang'] = $nm_cabang[0]->strnama_cabang;
					$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
					$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;
			
			
					$data['user'] = $this->session->userdata('username');
					$data['max_id'] = $this->getno_nota();
						
					$jpenjualan = $this->Penjualan_model->selectJPenjualanNotTrade();
					foreach ($jpenjualan as $g)
					{
						$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
						$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
					}
				$this->load->view('admin_views/penjualan/grand_after_pass',$data);
	}
	
	function cetak_nota_grand(){
		$max = $this->Penjualan_model->get_MaxNota()->result();
        $id = $max[0]->intid_nota;
        $ada = $this->Penjualan_model->get_CetakNota($id);
        $data['default'] = $this->Penjualan_model->get_CetakNota($id);
        $this->load->view('admin_views/penjualan/cetak_nota_grand', $data);
	}
	function nota_grand(){
        
        
		$this->form_validation->set_rules('intno_nota', 'No Nota', 'trim|required|xss_clean');
		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');
		if ($this->form_validation->run() == FALSE)
		{
            
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;


			$data['user'] = $this->session->userdata('username');
            $data['max_id'] = $this->getno_nota();
			
			$jpenjualan = $this->Penjualan_model->selectJPenjualan();
			foreach ($jpenjualan as $g)
			{
				$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
				$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
			}
			
			$this->load->view('admin_views/penjualan/nota', $data);
		 }else{
             
            $this->Penjualan_model->insertNotaHal($_POST,'grand');
            
            $max = $this->Penjualan_model->get_MaxNota()->result();
            $id = $max[0]->intid_nota;

			$data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
				if(!empty($data[$i]['intid_id'])){
				$detail = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data[$i]['intid_id'],
								'intquantity'		    => $data[$i]['intquantity'],
								'intid_harga'		    => $data[$i]['intid_id'],
								'intharga'				=> $data[$i]['intid_harga'],
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);
				 $this->Penjualan_model->add($detail);
				if($this->input->post('buat_arisan')!="arisan") {
				 $this->Penjualan_model->addStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
				}
				 }
            }

             $data_free = $this->input->post('barang_free');
				 if(sizeof($data_free) <= 1){
					 $d = sizeof($data_free) + 1;
				 }else{
					 $d = sizeof($data_free);
				 }
             for($i=1;$i<=30;$i++){
				 if(!empty($data_free[$i]['intid_id'])){
				 
				 $detail_free = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data_free[$i]['intid_id'],
								'intquantity'		    => $data_free[$i]['intquantity'],
								'is_free' 				=> 1,
								'intid_harga'			=> 0,
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);
				 $this->Penjualan_model->add($detail_free);
				if($this->input->post('buat_arisan')!="arisan") {
				 $this->Penjualan_model->addStok($data_free[$i]['intid_id'], $data_free[$i]['intquantity'], $this->input->post('intid_cabang'),$this->input->post('intno_nota'));
				}
				 }
               
		 	}

		//001
		$smartspending = $this->input->post('chkSmart');
		if ($smartspending == "on") {
			redirect('penjualan/cetak_nota_asi');
		}
		
			
            if($this->input->post('radio')==6){
                $ci = '5';
            }else{
                $ci='7';
            }

            //pilih cicilan
            if($this->input->post('buat_arisan')=="arisan"){

                    $arisan = array(
                                    'intid_arisan_detail' 	=> $id,
                                    'intuang_muka'		    => $this->input->post('intuangmukahide'),
                                    'intcicilan'            => $this->input->post('intcicilanhide'),
                                    'intjeniscicilan'		=> $ci,
                                    'group'                 => $this->input->post('group')
                                    );
                    $this->Penjualan_model->add_arisan($arisan);
                    $id_arisan =$this->db->insert_id();
                    $bulan = date('m');
                    $tahun = date('Y');
                    $ket = 'Pembayaran Cicilan langsung Ketika Daftar';
                    $ket_um = 'Pembayaran Uang Muka';
                    $this->Penjualan_model->add_arisan_detail_um($id_arisan, $ket_um);
                    $cicil_cek = $this->input->post('intc');
                    for ($v=1;$v<=7;$v++){
                         if(!empty($cicil_cek[$v])){
                            $this->Penjualan_model->add_arisan_detail_langsung($id_arisan, $ket);
                         }
                    }
			 }
            
            
            redirect('penjualan/cetak_nota_grand');
			
		}
    }
	////ending
	
	function mini()
	{
		$data['error_message'] = "";
		$temp = $this->session->userdata('username');
					
		if($this->input->post('grandpass')){
					$data['result'] = $this->Penjualan_model->get_password("mini", $this->input->post('grandpass'));
					
					if($data['result'] > 0 ){
					//echo $this->session->userdata('username')."<br/>";
					$cabang = $this->User_model->getCabang($this->session->userdata('username'));
					$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
			
					$data['cabang'] = $nm_cabang[0]->strnama_cabang;
					$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
					$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;
			
			
					$data['user'] = $this->session->userdata('username');
					$data['max_id'] = $this->getno_nota();
						
					$jpenjualan = $this->Penjualan_model->selectJPenjualanNotTrade();
					foreach ($jpenjualan as $g)
					{
						$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
						$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
					}
				$this->load->view('admin_views/penjualan/mini_after_pass',$data);
				}else{
					$data['error_message'] = "<div class='error_message'> login salah</div>";
					$this->load->view('admin_views/penjualan/mini', $data);
					}
		}else{
			$this->load->view('admin_views/penjualan/mini', $data);
		}
	}

	function seluruh_nota()
	{
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        	$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

        	$data['cabang'] = $nm_cabang[0]->strnama_cabang;
        	$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;


		$data['user'] = $this->session->userdata('username');
        	$data['max_id'] = $this->getno_nota();
			
		$jpenjualan = $this->Penjualan_model->selectJPenjualanPromo();
		foreach ($jpenjualan as $g)
		{
			$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
			$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
		}
		$this->load->view('admin_views/penjualan/seluruh_nota', $data);
	}

/////////////////////////////////////////////////////////////////////////////
	function lookuptracker003(){
		$keyword = $this->input->post('id_barang');
		if($keyword == 967){
			$total = 3;
		}elseif($keyword == 966){
			$total = 1;
		}else{
			$total = $this->Penjualan_model->selectfortracker003($keyword);
		}
		echo ($total); 
		
	}

function lookupNamaBarang_2(){
	$id = $this->input->post('id_barang');		
			
			$total = $this->Penjualan_model->selectNamaBarangB($id);			
			foreach($total as $rok){
				}
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $rok->intharga_jawa;
                }else{
                    $hrg = $rok->intharga_luarjawa;
                }
		    echo json_encode(array(
					'id'=>$rok->intid_barang,
					'value'=>$rok->strnama,
                                        'value1' => $hrg,
                                        'value7' => $rok->intid_harga,
                                     ));         
	}
//////////////////////////////////////////////////30Maret2013 line ikhlas

function gubrak(){
		$this->form_validation->set_rules('intno_nota', 'No Nota', 'trim|required|xss_clean');
		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');
		if ($this->form_validation->run() == FALSE)
		{
            
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;


			$data['user'] = $this->session->userdata('username');
            $data['max_id'] = $this->getno_nota();
			
			$jpenjualan = $this->Penjualan_model->selectJPenjualan();
			foreach ($jpenjualan as $g)
			{
				$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
				$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
			}
			
			$this->load->view('admin_views/penjualan/nota_gubrak', $data);
		 }else{
             
            $this->Penjualan_model->insertNota($_POST);
            
            $max = $this->Penjualan_model->get_MaxNota()->result();
            $id = $max[0]->intid_nota;

			$data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
				if(!empty($data[$i]['intid_id'])){
				$detail = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data[$i]['intid_id'],
								'intquantity'		    => $data[$i]['intquantity'],
								'intid_harga'		    => $data[$i]['intid_id'],
								'intharga'				=> $data[$i]['intid_harga'],
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);
				 $this->Penjualan_model->add($detail);
				if($this->input->post('buat_arisan')!="arisan") {
				 $this->Penjualan_model->addStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
				}
				 }
            }

             $data_free = $this->input->post('barang_free');
				 if(sizeof($data_free) <= 1){
					 $d = sizeof($data_free) + 1;
				 }else{
					 $d = sizeof($data_free);
				 }
             for($i=1;$i<=30;$i++){
				 if(!empty($data_free[$i]['intid_id'])){
				 
				 $detail_free = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data_free[$i]['intid_id'],
								'intquantity'		    => $data_free[$i]['intquantity'],
								'is_free' 				=> 1,
								'intid_harga'			=> 0,
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);
				 $this->Penjualan_model->add($detail_free);
				if($this->input->post('buat_arisan')!="arisan") {
				 $this->Penjualan_model->addStok($data_free[$i]['intid_id'], $data_free[$i]['intquantity'], $this->input->post('intid_cabang'),$this->input->post('intno_nota'));
				}
				 }
               
		 	}
			
            if($this->input->post('radio')==6){
                $ci = '5';
            }else{
                $ci='7';
            }

            //pilih cicilan
            if($this->input->post('buat_arisan')=="arisan"){

                    $arisan = array(
                                    'intid_arisan_detail' 	=> $id,
                                    'intuang_muka'		    => $this->input->post('intuangmukahide'),
                                    'intcicilan'            => $this->input->post('intcicilanhide'),
                                    'intjeniscicilan'		=> $ci,
                                    'group'                 => $this->input->post('group')
                                    );
                    $this->Penjualan_model->add_arisan($arisan);
                    $id_arisan =$this->db->insert_id();
                    $bulan = date('m');
                    $tahun = date('Y');
                    $ket = 'Pembayaran Cicilan langsung Ketika Daftar';
                    $ket_um = 'Pembayaran Uang Muka';
                    $this->Penjualan_model->add_arisan_detail_um($id_arisan, $ket_um);
                    $cicil_cek = $this->input->post('intc');
                    for ($v=1;$v<=7;$v++){
                         if(!empty($cicil_cek[$v])){
                            $this->Penjualan_model->add_arisan_detail_langsung($id_arisan, $ket);
                         }
                    }
			 }
            
            
            redirect('penjualan/cetak_nota');
			
		}
	}
/////////////line ikhlas 30032013
	function lookupBarangGubrak(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Penjualan_model->selectBarangGubrak($keyword);
		if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {

                 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
					$um = $row->intum_jawa;
					$cicilan = $row->intcicilan_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
					$um = $row->intum_luarjawa;
					$cicilan = $row->intcicilan_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->promo,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
										'value5' => $um,
										'value6' => $cicilan,
                                        'value7' => $row->intid_harga,
                                     );
            }

        }


        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	///////////end 30032013
	
//////////////////////////////////////////////////02042013 line ikhlas
	function gubrak_pass(){
	//jangan lupa dibuka kembali tanda komentar nya setelah promo beres.
	/*
	$data['error_message'] ="";
	if($this->input->post('gubrakpass')){
		$data['result'] = $this->Penjualan_model->get_password("gubrak", $this->input->post('gubrakpass'));
					if($data['result'] > 0){
					redirect('penjualan/gubrak_1');
					//$this->gubrak_1();
				}else{
					$data['error_message'] = "<div class='error_message'> login salah</div>";
					$this->load->view('admin_views/penjualan/gubrak', $data);
					}
		}else{
			$this->load->view('admin_views/penjualan/gubrak', $data);					
			}
	*/
	redirect('penjualan/gubrak_1');
	}	
//////////////////////////////////////////////////02042013 line ikhlas
function gubrak_1(){
		$this->form_validation->set_rules('intno_nota', 'No Nota', 'trim|required|xss_clean');
		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');
		if ($this->form_validation->run() == FALSE)
		{
            
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;


			$data['user'] = $this->session->userdata('username');
            $data['max_id'] = $this->getno_nota();
			
			$jpenjualan = $this->Penjualan_model->selectJPenjualan();
			foreach ($jpenjualan as $g)
			{
				$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
				$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
			}
			
			$this->load->view('admin_views/penjualan/nota_gubrak', $data);
		 }else{
             
            $this->Penjualan_model->insertNota($_POST);
            
            $max = $this->Penjualan_model->get_MaxNota()->result();
            $id = $max[0]->intid_nota;

			$data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
				if(!empty($data[$i]['intid_id'])){
				$detail = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data[$i]['intid_id'],
								'intquantity'		    => $data[$i]['intquantity'],
								'intid_harga'		    => $data[$i]['intid_id'],
								'intharga'				=> $data[$i]['intid_harga'],
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);
				 $this->Penjualan_model->add($detail);
				if($this->input->post('buat_arisan')!="arisan") {
				 $this->Penjualan_model->addStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
				}
				 }
            }

             $data_free = $this->input->post('barang_free');
				 if(sizeof($data_free) <= 1){
					 $d = sizeof($data_free) + 1;
				 }else{
					 $d = sizeof($data_free);
				 }
             for($i=1;$i<=30;$i++){
				 if(!empty($data_free[$i]['intid_id'])){
				 
				 $detail_free = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data_free[$i]['intid_id'],
								'intquantity'		    => $data_free[$i]['intquantity'],
								'is_free' 				=> 1,
								'intid_harga'			=> 0,
								'nomor_nota'			=> $data[$i]['nomor_nota']
								);
				 $this->Penjualan_model->add($detail_free);
				if($this->input->post('buat_arisan')!="arisan") {
				 $this->Penjualan_model->addStok($data_free[$i]['intid_id'], $data_free[$i]['intquantity'], $this->input->post('intid_cabang'),$this->input->post('intno_nota'));
				}
				 }
               
		 	}
			
            if($this->input->post('radio')==6){
                $ci = '5';
            }else{
                $ci='7';
            }

            //pilih cicilan
            if($this->input->post('buat_arisan')=="arisan"){

                    $arisan = array(
                                    'intid_arisan_detail' 	=> $id,
                                    'intuang_muka'		    => $this->input->post('intuangmukahide'),
                                    'intcicilan'            => $this->input->post('intcicilanhide'),
                                    'intjeniscicilan'		=> $ci,
                                    'group'                 => $this->input->post('group')
                                    );
                    $this->Penjualan_model->add_arisan($arisan);
                    $id_arisan =$this->db->insert_id();
                    $bulan = date('m');
                    $tahun = date('Y');
                    $ket = 'Pembayaran Cicilan langsung Ketika Daftar';
                    $ket_um = 'Pembayaran Uang Muka';
                    $this->Penjualan_model->add_arisan_detail_um($id_arisan, $ket_um);
                    $cicil_cek = $this->input->post('intc');
                    for ($v=1;$v<=7;$v++){
                         if(!empty($cicil_cek[$v])){
                            $this->Penjualan_model->add_arisan_detail_langsung($id_arisan, $ket);
                         }
                    }
			 }
            
            
            redirect('penjualan/cetak_nota_gubrak');
			
		}
	}
		////////////////line ikhlas firlana 04April2013///////////////////////
	function cetak_nota_gubrak(){
        $max = $this->Penjualan_model->get_MaxNota()->result();
        $id = $max[0]->intid_nota;
        $ada = $this->Penjualan_model->get_CetakNota($id);
        $data['default'] = $this->Penjualan_model->get_CetakNota($id);
        $this->load->view('admin_views/penjualan/cetak_nota_gubrak', $data);
	}
	/////////////////////line ikhlas 03APril 2013////////////
	function lookupBarangFreegubrak(){
		$keyword = $this->input->post('term');
        $state = $this->input->post('state');
		$data['response'] = 'false';
        $query = $this->Penjualan_model->selectBarangFreeGubrak($keyword, $state);
		if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {

                 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->promo,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
                                     );
            }

        }


        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	///line ikhlas 24April//
//009
	/**
	* untuk menu sidebar hadiah rekrut
	* @param hadiah_rekrut
	*/
	function hadiah_rekrut()
	{
		//redirect('penjualan/maintanance');
		$idt = 0;
		$id = 0;					
		$temp = 0;
		$datanota = 0;
		$datanotadetail =0;
		
		if (!$_POST)
		{
			$cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['id_wilayah'] = $nm_cabang[0]->intid_wilayah;

			$data['user'] = $this->session->userdata('username');
            $data['max_id'] = $this->getno_nota();
			
			//$this->load->view('admin_views/penjualan/nota_lg', $data);
			$this->load->view('admin_views/penjualan/nota_hadiah_rekrut', $data);
		 }else{
			$data = $this->input->post('barang');
				$member = $this->input->post('id');
				if($this->input->post('totalrekrut001') == 2){
					$temp = 1;
				}elseif($this->input->post('totalrekrut001') == 3){
					$temp = 2;
				}elseif($this->input->post('totalrekrut001') >= 4){
					$temp = 3;
				}
				$week = $this->Penjualan_model->selectWeek();
				//pengecekan nota terseida//
				$count = $this->Penjualan_model->cek_nota($this->input->post('intno_nota'));
			if($count == 0){
				$intid_dealer = $this->Penjualan_model->get_iddealer($this->input->post('strkode_dealer'));
				//memasukan data ke dalam tabel nota
				$datanota = array(
							'intno_nota' => $this->input->post('intno_nota'),
								'intid_cabang' => $this->input->post('intid_cabang'),
								'datetgl' => date('Y-m-d'),
								'intid_dealer' => $intid_dealer,
								'intid_unit' => $this->input->post('id_unit'),
								'intid_week' => $week[0]->intid_week,
								'nomor_nota'			=> $data[$i]['nomor_nota']								
						);
				$id = $this->Penjualan_model->insertNotaRekrutcoba($datanota);
			
				//memasukan data ke dalam notadetail.
				for($i=1;$i <= $temp;$i++){
				
					if(!empty($data[$i]['intid_barang'])){
						$datanotadetail = array(
							'intid_nota' => $id,
							'intid_barang' => $data[$i]['intid_barang'],
							'intquantity' => '1',
							'ket' => md5('REKRUTHADIAH')
							);
						$idt = $this->Penjualan_model->insertNotaRekrutdetail($datanotadetail);
						}
					}
				//data tabel rekrut hadiah detail berubah sesuai jumlah yang keluar di halaman sebelumnya.
				for($j=1;$j <= $this->input->post('totalrekrut001');$j++){
					$id_group = $this->Penjualan_model->updaterekrut_hadiah_detail($member[$j],$this->input->post('intno_nota'));
					$temp = $member[$j];			
				}
				//kalau sudah empat personeil maka status rekrut hadiah berubah.
				if($this->input->post('totalrekrut001') == 4){
					//echo "member : ".$member[$j];
					$kode = $this->Penjualan_model->selectUplinefromKode($temp);
					//echo "kode : ".$kode[0]->strkode_upline."<br />";
					
					//kasih status group untuk pemasukan sesuai id. 
					//$this->Penjualan_model->updaterekrut_hadiah($kode[0]->strkode_upline,$this->input->post('intno_nota'),$id_group);
					$this->Penjualan_model->updaterekrut_hadiah($kode[0]->strkode_upline,$this->input->post('intno_nota'),$id_group[0]->id_group);						
				}
				 redirect('penjualan/cetak_notahadiah');
			}else{
				
				 redirect('penjualan/cetak_notahadiah');
			}	
		}
	}
	/**
	* Kalau terjadi sesuatu misalkan check rekrut dibalikan kondisi semula,
	* maka fungsi check_rekrut dibuka dan fungsi check_rekrut_pod dan check_rekrut_mede di hidden
	* @param check_rekrut
	* @param check_rekrut_pod
	* @param check_rekrut_mede
	*
	function check_rekrut() {
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
           		$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            	$id_cabang = $nm_cabang[0]->intid_cabang;
				$id = $this->Penjualan_model->getidDealer($this->input->post('strkode_dealer'));
				//$data['omset'] = $this->Penjualan_model->getOmset($id[0]->intid_dealer, $id_cabang);
				$data['rekrut'] = $this->Penjualan_model->get_rekrut_hadiah($id[0]->intid_dealer);
				
				$this->load->view('admin_views/penjualan/rekrut', $data);
		}
		*/
	function check_rekrut_pod() {
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
           		$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            	$id_cabang = $nm_cabang[0]->intid_cabang;
				$id = $this->Penjualan_model->getidDealer($this->input->post('strkode_dealer'));
				//$data['omset'] = $this->Penjualan_model->getOmset($id[0]->intid_dealer, $id_cabang);
				$data['rekrut'] = $this->Penjualan_model->get_rekrut_hadiah($id[0]->intid_dealer,215000);
				$data['judul'] = "POWER OF DREAM";
				$this->load->view('admin_views/penjualan/rekrut', $data);
		}
	function check_rekrut_mede() {
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
           		$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            	$id_cabang = $nm_cabang[0]->intid_cabang;
				$id = $this->Penjualan_model->getidDealer($this->input->post('strkode_dealer'));
				//$data['omset'] = $this->Penjualan_model->getOmset($id[0]->intid_dealer, $id_cabang);
				$data['rekrut'] = $this->Penjualan_model->get_rekrut_hadiah($id[0]->intid_dealer,108000);
				$data['judul'] = "Make Everyday Exciting";
				$this->load->view('admin_views/penjualan/rekrut', $data);
		}	
	
	/**
	* Pencarian lookup tabel per group masih kurang bagus
	* @param lookupBarangGroupSatu
	* @param lookupBarangGroupDua
	* @param lookupBarangGroupTiga
	* @param lookupBarangGroupSatu108
	* @param lookupBarangGroupDua108
	* @param lookupBarangGroupTiga108
	*/
	function lookupBarangGroupSatu(){
		$keyword = $this->input->post('term');
		$data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectBarangRekrutHadiahSatu($keyword); 
		if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
					$um = $row->intum_jawa;
					$cicilan = $row->intcicilan_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
					$um = $row->intum_luarjawa;
					$cicilan = $row->intcicilan_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->strnama,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
										'value5' => $um,
										'value6' => $cicilan,
                                        'value7' => $row->intid_harga,
                                     );
            }
			           
        }
		
		 
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }	
	}
	function lookupBarangGroupDua(){
		$keyword = $this->input->post('term');
		$data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectBarangRekrutHadiahDua($keyword); 
		if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
					$um = $row->intum_jawa;
					$cicilan = $row->intcicilan_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
					$um = $row->intum_luarjawa;
					$cicilan = $row->intcicilan_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->strnama,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
										'value5' => $um,
										'value6' => $cicilan,
                                        'value7' => $row->intid_harga,
                                     );
            }
			           
        }
		
		 
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	function lookupBarangGroupTiga(){
		$keyword = $this->input->post('term');
		$data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectBarangRekrutHadiahTiga($keyword); 
		if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
					$um = $row->intum_jawa;
					$cicilan = $row->intcicilan_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
					$um = $row->intum_luarjawa;
					$cicilan = $row->intcicilan_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->strnama,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
										'value5' => $um,
										'value6' => $cicilan,
                                        'value7' => $row->intid_harga,
                                     );
            }
			           
        }
		
		 
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	function lookupBarangGroupSatu108(){
		$keyword = $this->input->post('term');
		$data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectBarangRekrutHadiahSatu108($keyword); 
		if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
					$um = $row->intum_jawa;
					$cicilan = $row->intcicilan_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
					$um = $row->intum_luarjawa;
					$cicilan = $row->intcicilan_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->strnama,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
										'value5' => $um,
										'value6' => $cicilan,
                                        'value7' => $row->intid_harga,
                                     );
            }
			           
        }
		
		 
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }	
	}
	function lookupBarangGroupDua108(){
		$keyword = $this->input->post('term');
		$data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectBarangRekrutHadiahDua108($keyword); 
		if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
					$um = $row->intum_jawa;
					$cicilan = $row->intcicilan_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
					$um = $row->intum_luarjawa;
					$cicilan = $row->intcicilan_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->strnama,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
										'value5' => $um,
										'value6' => $cicilan,
                                        'value7' => $row->intid_harga,
                                     );
            }
			           
        }
		
		 
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	function lookupBarangGroupTiga108(){
		$keyword = $this->input->post('term');
		$data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectBarangRekrutHadiahTiga108($keyword); 
		if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
					$um = $row->intum_jawa;
					$cicilan = $row->intcicilan_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
					$um = $row->intum_luarjawa;
					$cicilan = $row->intcicilan_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->strnama,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
										'value5' => $um,
										'value6' => $cicilan,
                                        'value7' => $row->intid_harga,
                                     );
            }
			           
        }
		
		 
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	function maintanance(){
		$this->load->view('admin_views/maintanance');
	}
	
	function cetak_arisan_test(){
        $id = $this->uri->segment(3);
       $data['default'] = $this->Penjualan_model->get_CetakArisan($id);
		$data['no_nota'] = $this->uri->segment(4);
		$this->load->view('admin_views/penjualan/cetak_arisan', $data);
	}
	function lookupBarangLg1(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Penjualan_model->selectBarangLg1($keyword);
		if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                   
                }else{
                    $hrg = $row->intharga_luarjawa;
                    
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->lg,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
									);
            }

        }


        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	function lookupBarangLg2(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Penjualan_model->selectBarangLg2($keyword);
		if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                   
                }else{
                    $hrg = $row->intharga_luarjawa;
                    
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->lg,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
									);
            }

        }


        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	function lookupBarangLg3(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Penjualan_model->selectBarangLg3($keyword);
		if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                   
                }else{
                    $hrg = $row->intharga_luarjawa;
                    
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->lg,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
									);
            }

        }


        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	function lookupUplineNonManager(){
		$keyword = $this->input->post('term');
		$unit = $this->input->post('state');
		$data['response'] = 'false'; 
		$query = $this->Penjualan_model->selectUplineNonManager($keyword, $unit); 
        if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array( 
                                        'id'=>$row->strkode_dealer,
                                        'value' => strtoupper($row->strnama_dealer),
                                        'value1' => $row->strkode_upline,
					'value2' => $row->strnama_upline,
					'value3' => $row->intlevel_dealer,
					'value4' => $row->steamer,
					'value5' => $row->emc,
					'value6' => $row->chooper,
					'value7' => $row->metal_5lt,
					'value8' => $row->metal_7lt,
					'value9' => $row->metal_7in1,
                                     );  
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
	//** diperuntukkan untuk halaman promo1free1
	function promo1free1()
	{
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
        $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$data['intid_wilayah'] = $nm_cabang[0]->intid_wilayah;


		$data['user'] = $this->session->userdata('username');
        $data['max_id'] = $this->getno_nota();
			
		$jpenjualan = $this->Penjualan_model->selectJPenjualanPromo1free1();
		foreach ($jpenjualan as $g)
		{
			$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
			$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
		}
		$this->load->view('admin_views/penjualan/promo1free1', $data);
	}
	////////////////////////////////////
function lookupBarangSatuFreeSatuBayar(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false'; 
        $query = $this->Penjualan_model->selectBarangSatuFreeSatuBayar($keyword,'1fre1'); 
		if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
					$um = $row->intum_jawa;
					$cicilan = $row->intcicilan_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
					$um = $row->intum_luarjawa;
					$cicilan = $row->intcicilan_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->strnama,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
										'value5' => $um,
										'value6' => $cicilan,
                                        'value7' => $row->intid_harga,
                                     );
            }
			           
        }
		
		 
        if('IS_AJAX')
        {
            echo json_encode($data); 
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
//////////////////////////////////////////////////////////
function lookupBarangSatuFreeSatu(){
		$keyword = $this->input->post('term');
        $state = $this->input->post('state');
		$key	=	$this->input->post('key');
		$data['response'] = 'false';
        $query = $this->Penjualan_model->selectBarangSatuFreeSatu($keyword, $state, $key);
		if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {

                 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
                if($cabang[0]->intid_wilayah == 1){
                    $hrg = $row->intharga_jawa;
                    $pv = $row->intpv_jawa;
                }else{
                    $hrg = $row->intharga_luarjawa;
                    $pv = $row->intpv_luarjawa;
                }
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
										'value'=>$row->promo,
                                        'value1' => $hrg,
										'value2' => $row->intharga_luarjawa,
										'value3' => $pv,
										'value4' => $row->intpv_luarjawa,
                                     );
            }
        }


        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
////////////////////////////////////////////////
function nota_vers2(){
		$id_nota = "";
		$size = $this->input->post('tracker001');
		$temp = array();
		$this->load->model('mamal_model');
		$week = $this->Penjualan_model->selectWeek();
        $intid_week= $week[0]->intid_week;
		if(isset($_POST['intdp'])){
			$is_dp = 1;
			$cash = $this->input->post('intdp');
		} else { 
			$is_dp = 0;
			$cash = $this->input->post('intcash');
		}
		if(isset($_POST['is_lg'])){
			$is_lg = 1;
		} else {
			$is_lg = 0;
		}
		if(isset($_POST['is_asi'])){
			$is_asi = 1;
			$kredit = $this->input->post('totalbayarhidden');
			$komisiasi = $this->input->post('intkomisiasi');
		} else {
			$is_asi = 0;
			$kredit = $this->input->post('intkkredit');
			$komisiasi = 0;
		}
		$data['intno_nota']=$this->input->post('intno_nota');
		$data['intid_jpenjualan']=$this->input->post('intid_jpenjualan');
		$data['intid_cabang']=$this->input->post('intid_cabang');
		$data['intid_dealer']=$this->input->post('intid_dealer');
		$data['intid_unit']=$this->input->post('id_unit');
		$data['datetgl']=date('Y-m-d');
		$data['intid_week']=$intid_week;
		$data['intomset10']=$this->input->post('jml10hidden');
		$data['intomset20']=$this->input->post('jml20hidden');
		$data['inttotal_omset']=$this->input->post('jumlahOmsethidden');
		$data['inttotal_bayar']=$this->input->post('totalbayarhidden');
		$data['intdp']=$this->input->post('intdp');
		$data['intcash']=$cash;
		$data['intdebit']=$this->input->post('intdebit');
		$data['intkkredit']=$this->input->post('intkkredit');
		$data['intsisa']=$this->input->post('intsisahidden');
		$data['intkomisi10']=$this->input->post('komisi1hidden');
		$data['intkomisi20']=$this->input->post('komisi2hidden');
		$data['intpv']=$this->input->post('intpv');
		$data['intvoucher']=$this->input->post('intvoucher');
		$data['is_dp']=$is_dp;
		$data['inttrade_in']=$this->input->post('komisitrade');
		$data['is_lg']=$is_lg;
		$data['is_asi']=$is_asi;
		$data['nokk']=$this->input->post('nokk');
		$data['intkomisi_asi']=$komisiasi;
		$data['is_arisan']= $this->input->post('is_arisan');
		$data['halaman']=$this->input->post('halaman');
		/*
		for($i = 1; $i<=sizeof($data);$i++){
			echo $i.", ".$data[$i]."<br />";
		}
		*/
		$id_nota = $this->mamal_model->elephant($data,'nota',1);
		//echo "id ;".$id;
		if(!empty($id_nota)){
			$barang = $this->input->post('barang');
			//echo "length ; ".sizeof($barang)."<br />";
			for($cont = 1;$cont <= $size;$cont++){
				 if(!empty($barang[$cont]['intquantity'])){
				 	$temp[$cont][0] = $id_nota;
				 	$temp[$cont][1] = $barang[$cont]['intid_barang'];
				 	$temp[$cont][2] = $barang[$cont]['intquantity'];
					if($barang[$cont]['harga'] == 0){
								$temp[$cont][3]	= 0;
								$temp[$cont][4]	= 1;
							}else{
								$temp[$cont][3] = $barang[$cont]['intid_barang'];
								$temp[$cont][4]	= 0;
							}
				 	$temp[$cont][5] = $barang[$cont]['harga'];
				 	}
				}
				/*
				for(){
					echo "temp, ".."".."".."".."".."".."".."";
				}
				*/
					///baru sistem memasukan
					for($cont = 1;$cont <= sizeof($temp);$cont++){
						$data_detail = array();
						if(!empty($barang[$cont]['intquantity'])){
							$data_detail['intid_nota']	= $id_nota;
							$data_detail['intid_barang'] = $temp[$cont][1];
							$data_detail['intquantity']	= $temp[$cont][2];
							if($barang[$cont]['harga'] == 0){
								$data_detail['intid_harga']	= $temp[$cont][3];
								$data_detail['is_free']	= $temp[$cont][4];
							}else{
								$data_detail['intid_harga'] = $temp[$cont][3];
								$data_detail['is_free']	= $temp[$cont][4];
							}
							$data_detail['intharga'] = $temp[$cont][5];
							$id = $this->mamal_model->elephant($data_detail,'nota_detail',1);
							//echo "intid_nota : ".$data_detail['intid_nota'].", intid_barang : ".$data_detail['intid_barang'].", intquantity : ".$data_detail['intquantity'].",intid_harag ".$data_detail['intid_harga'].", nominal ".$data_detail['intharga']."<br />";
							if(empty($id)){
								 $this->session->set_flashdata('messages', '<script>alert("PERINGATAN! terjadi Gangguan pada penginputan barang.");</script>');
								}	
							}	
				}
			redirect('penjualan/cetak_nota');
			
		}else{
				$this->session->set_flashdata('messages', '<script>PERINGATAN! terjadi Gangguan pada penginputan Nota.</script>');
		}
	}
	////////////////////////////////////////
}
?>